/**
 * Created by smp on 2017/4/24.
 */
var isSearchBarExpand = false;
var index;

var modelPath=ROOT_PATH+'/majorHazard/technicalReport/';
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=parent.layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['1150px', '70%'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
/**
 * 打开详情页面
 */
function openDetailPage(id){
    WindowManager.openLayer({
        type: 2,
        title: '登记信息',
        area: ['1150px', '70%'],
        content: modelPath + 'detail.shtml?id='+id
    });
}
/***
 * 根据业务id查询流程信息
 * @param row
 */
function flowMonitorByBusinessId(businessId) {
    var index = layer.open({
        type: 2,
        title: '业务流程监控',
        area: ['80%', '80%'],
        content: ROOT_PATH + '/workflow/flowManagement/flowMonitorByBusinessId.shtml?businessId='+businessId
    });
}
$(function () {
    $('#deptId').combotree({
        url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
    });
    var getSelectedUser2 = function (data) {
        var id=data[0].id;
        var realName=data[0].realName;
        /*for(var i=1;i<data.length;i++){
         id=id+','+data[i].id;
         realName=realName+','+data[i].realName;
         }*/
        $("#addUserId").val(id);
        $("#addUserName").textbox("setValue",realName);

    };

    $('#addUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                WindowManager.openLayer({
                    type: 2,
                    title: '添加承包人',
                    area: ['90%', '70%'],
                    content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                    callBack:getSelectedUser2
                });
            }
        }],
        editable: false
    });
            //初始化加载datagrid
            $('#dg1').datagrid({
                method: 'post',
                url: modelPath + 'list.shtml',
                queryParams: {
                    sort: 'addTime',
                    order: 'desc'
                },
                // idField: 'id',
                fitColumns: true,
                pagination: true,
                rownumbers: true,
                singleSelect:true,
                sortOrder: true,
                striped: true,
                fit: true,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'technicalReport', title: '报告名称',formatter:function(value,row,index){
                        return "<a href='javascript:void(0);' style='color: deepskyblue;' onclick='openDetailPage(\""+row.id+"\")'>"+value+"</a>"
                    }},
                    {field: 'criticalParts', title: '关键装置/要害部位', width: 100,  sortable: true},
                    {field: 'year', title: '年度', width: 80,  sortable: true},
                    {field: 'addUserName', title: '登记人', width: 100 },
                    {field: 'addTime', title: '登记时间', width: 100 },
                    {field: 'addDeptName', title: '登记部门', width: 80},
                    /*{field: 'enclosure', title: '技术报告附件', width: 80, sortable: true},*/
                    {field: 'state', title: '状态', width: 100,
                        formatter:function(value,row,index){
                                if(value=="未提交"){
                                    return "<span style='color: #FF0000;'>"+value+"</span>";
                                }else if(value=="未审核"){
                                    return "<a href='javascript:void(0);' onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color: #0abffc;'>"+value+"</a>";
                                }else{
                                    return "<a href='javascript:void(0);' onclick=\"flowMonitorByBusinessId('"+row.id+"')\" style='color: #FF8C00'>"+value+"</a>"
                                }
                        }
                    }
                ]],
                toolbar: '#toolbar'
            });
    //添加按钮事件
    $('#majorHazard_technical_add').on('click',function () {

        var title='添加技术报告';
        var content='add.shtml?resourceId=' + $.getQueryParam('resourceId');
        openLayer(title,content);
    });
    //编辑按钮事件
    $('#majorHazard_technical_edit').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        var title='编辑技术报告';
        var content='edit.shtml?id='+id+'&resourceId=' + $.getQueryParam('resourceId');
        openLayer(title,content);
    });
    //删除按钮事件
    $('#majorHazard_technical_delete').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要删除的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var id=checkedRows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: modelPath+'delete.shtml',
                data: {id:id,delState:1},
                dataType: "json",
                success: function(result){
                    if (result.success) {
                        $('#dg1').datagrid('clearSelections').datagrid('reload');
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });
    /**
     * 高级查询列表
     */
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    /**
     * 查询按钮
     */
    $('#selectTechnicalReport').on('click',function(){
        var deptId=$('#deptId').textbox("getValue");
        var technicalReport=$('#technicalReport').textbox("getValue");
        var year=$('#year').textbox("getValue");
        var criticalParts=$('#criticalParts').textbox("getValue");
        var beginTime=$('#beginTime').datebox("getValue");
        var endTime=$('#endTime').datebox("getValue");
        var state=$('#state').textbox("getValue");
        var addUserId=$('#addUserId').val();
        $('#dg1').datagrid('load', {deptId:deptId,technicalReport:technicalReport,year:year,criticalParts:criticalParts,beginTime:beginTime,endTime:endTime,state:state,addUserId:addUserId,sort: 'addTime', order: 'desc'});
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset');
    });


    //提交按钮事件
    $('#majorHazard_technical_submit').on('click',function () {

        var checkedRows=$('#dg1').datagrid('getChecked');
        if(checkedRows.length === 0){
            layer.alert("选择需要提交的行！");
            return
        }
        if(checkedRows[0].state !== "未提交"){
            layer.alert("当前数据已在流程当中！");
            return
        }

        var id=checkedRows[0].id;
        var confirmIndex=layer.confirm('是否确定提交审批？', function () {
            layer.close(confirmIndex);
            /***
             * 选择处理人
             */
            WindowManager.openLayer({
                type: 2,
                title: '选择下一环节处理人',
                area: ['370px', '180px'],
                content: ROOT_PATH + '/workflow/flowManagement/toFlowNextUserSelectPage.shtml',
                data:{id:'ff',name:'我是名称'},
                callBack:function(data){//窗口关闭后的回调函数
                    //得到选择的人员
                    if(data==null||data===""){
                        layer.alert("请选择处理人!");
                        return false;
                    }
                    $.ajax({
                        type: "POST",
                        url: modelPath+'editSubmit.shtml',
                        data: {id: id,nextUserId:data,state:"未审核"},
                        dataType: "json",
                        success: function (data) {
                            if(data.success){
                                layer.msg(data.message,{time:1000,icon:1},function () {
                                    $('#dg1').datagrid('reload');
                                });
                            }else{
                                layer.msg(data.message,{time:1000,icon:2});
                            }
                        }
                    });
                }
            });
        });
    });
});